﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CCH.DAO;
using CCH.Entity;
using CCH.Lib;
using System.Drawing.Imaging;
using System.Drawing;

public partial class _20_2004_5 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            sysfunctionDAO dao = new sysfunctionDAO();

            //取回功能編號
            string sfu_type = Request.QueryString["type"];
            int sfu_no = new sysfunctionDAO().Get_pathNO(2004, sfu_type);

            this.hidd_no.Value = Request.QueryString["alb_no"];

            this.lab_title.Text = dao.Get_titleName(sfu_no) + " / " + new albumDAO().Get_album(int.Parse(this.hidd_no.Value)).alb_name + " - 照片上傳";

            
        }
    }

    protected void Page_LoadComplete(object sender, EventArgs e)
    {
        this.TextBox1.Focus();
    }

    protected void btn_ok_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(this.hidd_no.Value))
        {
            //上傳路徑
            string root = System.Configuration.ConfigurationManager.AppSettings["peoplePIC_PATH"];
            int peo_uid = new SessionObject().SessionUserID;
            int alb_no = int.Parse(this.hidd_no.Value);
            string[] temp = { "jpeg", "jpg", "bmp", "png" };
            albumDAO dao = new albumDAO();

            for (int i = 1; i <= 10; i++)
            {
                FileUpload myFile = (FileUpload)this.FindControl("FileUpload" + i);
                TextBox myText = (TextBox)this.FindControl("TextBox" + i);

                if (myFile != null && myFile.HasFile)
                {
                    //副檔名
                    string exi = myFile.FileName.Split('.')[myFile.FileName.Split('.').Length - 1].ToLower();

                    if (temp.Contains(exi))
                    {
                        //檔名
                        string filename = root + Guid.NewGuid().ToString("N") + "." + exi;

                        //上傳
                        myFile.SaveAs(filename);

                        //做縮圖
                        string thumbFile = this.ChangeImageSize(root, exi, filename);

                        photo p = new photo();
                        p.alb_no = alb_no;
                        p.pho_no = dao.Get_photo_Count(alb_no) + 1;
                        p.pho_name = myFile.FileName;
                        p.pho_desc = myText.Text;
                        p.pho_file = filename;//相片路徑
                        p.pho_thumb = thumbFile;//縮圖路徑
                        p.pho_createtime = DateTime.Now;
                        p.pho_createuid = peo_uid;
                        dao.Add_photo(p);
                    }
                }
            }

            //更新父代網頁
            JsUtil.AlertJs(this, "上傳完成!");
        }
    }

    /// <summary>
    /// 照片縮圖
    /// </summary>
    /// <param name="root"></param>
    /// <param name="exi"></param>
    /// <param name="FileName">原始圖片路徑</param>
    /// <returns>回傳縮圖路徑</returns>
    protected string ChangeImageSize(string root, string exi, string FileName)
    {

        string thumbFile = root + Guid.NewGuid().ToString("N") + "." + exi;

        System.Drawing.Image img = System.Drawing.Image.FromFile(FileName);
        ImageFormat ThisFormat = img.RawFormat;
        //圖片大小
        int FixWidth = 65;
        int FixHeight = 65;//FixWidth * img.Height / img.Width;

        Bitmap ImgOutput = new Bitmap(img, FixWidth, FixHeight);

        ImgOutput.Save(thumbFile, ThisFormat);

        img.Dispose();
        ImgOutput.Dispose();

        return thumbFile;

    }
}